import cloneDeep from "lodash.clonedeep"; //深拷贝
import pick from "lodash.pick"; //对象清理

function getPerm() {
  const userinfo = uni.getStorageSync("userinfo");
  return userinfo.permissions.split(",");
}

//过滤滑动单元格按钮权限
export function swipeActPer(options, status = null, mode = "opt") {
  const userPerm = getPerm();
  const hasSuperPerm = userPerm.includes("999999") || userPerm.includes("999");
  const optionV = cloneDeep(options).filter(
    (opt) => userPerm.includes(opt.per) || hasSuperPerm
  ); // 深拷贝一份
  let newOption = [];
  const filtered = optionV.filter((opt) => {
    if (!status) return true; // 无状态直接返回
    if (
      opt.text === "修改" &&
      (status === "待审核" || status === 0 || status === "审核中"|| status === "待接单")
    )
      return true;
    if (opt.text === "删除") return true;
    return false;
  });
  if (mode === "opt") {
    // 返回最终按钮列表，如果 filtered 为空，返回原始 options
    return filtered.length ? filtered : options;
  } else if (mode === "disabled") {
    // 返回是否禁用（true = 禁用）
    if (!status) return optionV.length === 0;
    return filtered.length === 0;
  }
}

//过滤详情跳转权限
export function goDetail(item, url, per) {
  const userPerm = getPerm();

  if (
    userPerm.includes(per) ||
    userPerm.includes("999") ||
    userPerm.includes("999999")
  ) {
    const id = item.id;
    uni.navigateTo({
      url: `${url}/detail?id=${id}`,
    });
  }
}

export function hasPerm(perms) {
  const userinfo = uni.getStorageSync("userinfo");
  const roles = userinfo.permissions.split(",");
  if (roles.includes("999") || roles.includes("999999")) return true;

  if (Array.isArray(perms)) {
    return perms.some((p) => roles.includes(p));
  }
  return roles.includes(perms);
}
export { cloneDeep, pick };
